package com.yoomiito.app.adapter.goods.detail;

import android.content.Context;
import androidx.annotation.NonNull;

import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.layout.LinearLayoutHelper;
import com.yoomiito.app.R;
import com.yoomiito.app.utils.ImageLoader;

import java.util.List;

public class GoodsDetailAdapter extends DelegateAdapter.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private List<String> picUrls;

    public GoodsDetailAdapter(Context context, List<String> picUrls) {
        mContext = context;
        this.picUrls = picUrls;
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return new LinearLayoutHelper();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//        ImageView iv = new ImageView(mContext);
//        iv.setFocusable(false);
//        iv.setFocusableInTouchMode(false);
//        iv.setEnabled(false);
//        RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
//                ViewGroup.LayoutParams.WRAP_CONTENT);
//        iv.setLayoutParams(lp);
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_iv,parent,false);
        return new ItemViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ItemViewHolder viewHolder = (ItemViewHolder) holder;
        String url = picUrls.get(position);
        if (!url.contains("http")&&!url.contains("https")){
            url = "http:"+url;
        }
        ImageLoader.getInstance().intoAllSize(mContext, url,viewHolder.iv,R.drawable.goods_default);
    }

    @Override
    public int getItemCount() {
        return picUrls.size();
    }

    @Override
    public int getItemViewType(int position) {
        return 3;
    }

    private class ItemViewHolder extends RecyclerView.ViewHolder{
        private ImageView iv;
        public ItemViewHolder(View itemView) {
            super(itemView);
//            iv = (ImageView) itemView;
            iv = itemView.findViewById(R.id.item_iv);
        }
    }
}
